﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LegacyAnimationHideLogic : MonoBehaviour
{

    public LegacyAnimation anim;

    public string aniName;

    [System.Serializable]
    public class VisibleCtrl
    {
        public float beginTime;
        public float endTime;
        public bool isShow;
    }

    public List<VisibleCtrl> ctrl;

    bool isRunning = false;

    float duration = 0f;
    float curDeltaTime = 0f;

    void Update()
    {
        if (anim.IsPlaying(aniName) && !isRunning)
        { 
            curDeltaTime = 0f;
            duration = anim.GetAniLength(aniName);
            isRunning = true;
        }
        else
        {
            if (!anim.IsPlaying(aniName))
            {
                isRunning = false;
            }
        }

        if (isRunning)
        {
            curDeltaTime += Time.deltaTime;
            foreach (var c in ctrl)
            {
                if (curDeltaTime >= c.beginTime && curDeltaTime < c.endTime)
                {
                    if (c.isShow && !isShow)
                    {
                        ShowModel();
                    }
                    else if (!c.isShow && isShow)
                    {
                        HideModel();
                    }
                    break;
                }
            }

            if (curDeltaTime >= duration)
            {
                anim.transform.localPosition = Vector3.zero;
                isRunning = false;
                ShowModel();
            }

        }


    }
    bool isShow = false;

    void HideModel()
    {
        isShow = false;
        var kit = anim.GetComponent<ModelRenderKit>();
        kit.skinnedMeshRenderer.gameObject.SetActive(false);
        if (kit.leftWeapon != null)
        {
            kit.leftWeapon.SetActive(false);
        }
        if (kit.rightWeapon != null)
        {
            kit.rightWeapon.SetActive(false);
        }
    }

    void ShowModel()
    {
        isShow = true;
        var kit = anim.GetComponent<ModelRenderKit>();
        kit.skinnedMeshRenderer.gameObject.SetActive(true);
        if (kit.leftWeapon != null)
        {
            kit.leftWeapon.SetActive(true);
        }
        if (kit.rightWeapon != null)
        {
            kit.rightWeapon.SetActive(true);
        }
    }
}
